﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Text.RegularExpressions;
using Project_Vital.Dao;
using Project_Vital.StringResources;


namespace Project_Vital
{
    public partial class CustomerDetails : Form
    {
        List<string> items = new List<string>() { "1-Lucia", "2-Mauricio", "3-Frank", "4-Dog" };
        ErrorProvider errorProvider = new ErrorProvider();
    
        public CustomerDetails()
        {
           InitializeComponent();

            this.Text = FormTitle.FormNewCus;

            btnCancel.Visible = false;
            btnPayments.Visible = false;
            btnEditDetails.Visible = false;
            btnViewPrevOrder.Visible = false;
            btnCreateOrder.Visible = false;
            btnViewInvoice.Visible = false;
            label24.Visible = false;

            SetupErrorProvider();
        }

        private void SetupErrorProvider()
        {
            this.errorProvider = new System.Windows.Forms.ErrorProvider();
            //this.errorProvider.SetIconAlignment(this.nameTextBox1, ErrorIconAlignment.MiddleRight);
            //this.errorProvider.SetIconPadding(this.nameTextBox1, 2);
            this.errorProvider.BlinkRate = 1000;
            this.errorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
        }
    
        private void tbCustEmail_Validating(object sender, CancelEventArgs e)
        {
            System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (tCustEmail.Text.Length > 0)
            {
                if (!rEMail.IsMatch(tbCustEmail.Text))
                {
                    this.errorProvider.SetError(tbCustEmail, "E-Mail expected");
                    tCustEmail.SelectAll();
                    e.Cancel = true;
                }
            }
        }
        
        public static bool ValidateUkPostcode(string postcode)
        {
         return Regex.Match(postcode, "(^gir\\s?0aa$)|(^[a-z-[qvx]](\\d{1,2}|[a-hk-y]\\d{1,2}|\\d[a-hjks-uw]|[a-hk-y]\\d[abehmnprv-y])\\s?\\d[a-z-[cikmov]]{2}$)", RegexOptions.IgnoreCase).Success;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            

            /*Surname, DoB, Contact Person, Business Name,Address 1, City, Postcode, Phone, Open Time, 
            Close Time are mandatory fields.*/

            if (string.IsNullOrEmpty(tCustSname.Text))
            {
                this.errorProvider.SetError(tCustSname, CustomerErrorStrings.SurNameMissing);
                return;
            }
            else if (string.IsNullOrEmpty( tcustDOB.Text))
            {
                this.errorProvider.SetError(tcustDOB , CustomerErrorStrings.DOBMissing);
                return;
            }
            else if (string.IsNullOrEmpty( tCustContP2.Text))
            {
                MessageBox.Show("You must enter a Contact Persons Name.");
                return;
            }
            else if (string.IsNullOrEmpty(tCustBName.Text))
            {
                this.errorProvider.SetError(tCustBName , CustomerErrorStrings.BusinessNameMissing);
                return;
            }
            else if (string.IsNullOrEmpty(tCustAdd1.Text))
            {
                this.errorProvider.SetError(tCustAdd1, CustomerErrorStrings.Address1Missing);
                return;
            }
            else if (string.IsNullOrEmpty(tbCustCity.Text))
            {
                MessageBox.Show("");
                return;
            }
            else if (string.IsNullOrEmpty(tPostCode.Text))
            {
                MessageBox.Show("");
                return;
            }
            else if (string.IsNullOrEmpty(tcustcontact.Text))
            {
                MessageBox.Show("");
                return;
            }
            else if (string.IsNullOrEmpty(CustOpenTime.Text))
            {
                MessageBox.Show("");
                return;
            }
            else if (string.IsNullOrEmpty(CustCloseTime.Text))
            {
                this.errorProvider.SetError(CustCloseTime, CustomerErrorStrings.ClosingTimeMissing);
                return;
            }
            else
            {
                CustomerDao customerDao = new CustomerDao();
                customerDao.CreateCustomer(this.CreateNewCustomer());
            }

        }

        private Customer CreateNewCustomer()
        {
            Customer customer = new Customer();
        //  customer._vcus_customer_id = "4";
            customer._vcus_title = cmboxTitle.Text;
            customer._vcus_first_name = tCustFame.Text;
            customer._vcus_surname = tCustSname.Text;
            customer._vcus_dob = tcustDOB.Text;
            customer._vcus_contact_name = tCustContP2.Text;
            customer._vcus_business_name = tCustBName.Text;
            customer._vcus_business_addr1 = tCustAdd1.Text;
            customer._vcus_business_addr2 = tCustAdd2.Text;
            customer._vcus_city =  tCustCity.Text;
            customer._vcus_postcode = tPostCode.Text;
            customer._vcus_phone = tcustcontact.Text;
            customer._vcus_mobile = tCustMob.Text;
            customer._vcus_fax = tCustFax.Text;
            customer._vcus_email = tCustEmail.Text;
            customer._vcus_open_time = CustOpenTime.Text;
            customer._vcus_close_time = CustCloseTime.Text;
            customer._vcus_account_start = dateTimePickerAccStart.Text;
            customer._vcus_account_end = dateTimePickerAccEnd.Text;
            customer._vcus_invoice = checkBoxInvoice.ToString();
            customer._vcus_pricing_level = cmbLevel.Text;
            customer._vcus_credit = "checkBoxCredit";
            customer._vcus_credit_limit = tCreditLimit.Text;
            customer._vcus_credit_period = tbCustCreditDays.Text;
            return customer;
        }



        /*Phone number and Mobile Number (If applicable) should also 
        be validated against correct UK format.*/

       
        private void tbCustContact_Validating(object sender, CancelEventArgs e)
        {
            Regex regexObj =
                new Regex(@"^(?:\+?0[-. ]?)?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$");

            if (regexObj.IsMatch(tcustcontact.Text))
            {
                string formattedPhoneNumber =
                    regexObj.Replace(tcustcontact.Text, "($1) $2-$3");
            }
            else
            {
                MessageBox.Show("Please enter Correct Number ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    tbCustEmail.SelectAll();
                    e.Cancel = true;
                
            } 
        }

        //Close Form
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //Open View Invoice form
        private void btnViewInvoice_Click(object sender, EventArgs e)
        {
            
           // 
           // Application.OpenForms[1].Show();
            ViewInvoice frm = new ViewInvoice();
            frm.Show();
            this.Visible = false;
            this.Close();

            }

        private void CustomerDetails_Load(object sender, EventArgs e)
        {
            cmboxTitle.Items.AddRange(items.ToArray());
            comboBox1.Items.Add(comboBox1.Items);

        }

        private void cmboxTitle_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

     
 }
}
